<?php
class M_Cruise extends Apps_Db_Table_Abstract {
    protected $_name = 'cruise';

    public function listCruiseByCategoryId($categoryId) {
        $q = $this->_db
                    ->select()
                    ->from('cruise', null)
                    ->join('cruise_category', 'cruise_category.id = cruise.cruise_category_id', null)
                    ->join('article', 'article.reference_id = cruise.id', null)
                    ->where('article.reference_type =? ', M_DbTable_Article::REFERENCE_TYPE_CRUISE)
                    ->where('cruise_category.id =? ', $categoryId)
                    ->where('cruise.status =? ', 1)
                    ->order('cruise.sorting_number ASC')
                    ->limit('3')
                    ->columns(array('id'                  => 'cruise.id',
                                    'thumbnail'           => 'article.thumbnail'));

        if($this->getLang() == 'en') {
            $q->columns(array('name'         => 'cruise.en_name',
                              'title'        => 'article.en_title',
                              'overview'     => 'article.en_overview'));
        } else $q->columns(array('name'      => 'cruise.name',
                                 'title'     => 'article.title',
                                 'overview'  => 'article.overview'));

        return $q;
    }

    public function listBestSellingCruiseByCategory($categoryId) {
    	$q = $this->_db
                    ->select()
                    ->from('cruise', null)
                    ->join('cruise_category', 'cruise_category.id = cruise.cruise_category_id', null)
                    ->join('cruise_type', 'cruise_type.id = cruise.cruise_type_id', null)
                    ->join('article', 'article.reference_id = cruise.id', null)
                    ->where('cruise_category.id = ?', $categoryId)
                    ->where('cruise.best_selling = ?', 1)
                    ->where('cruise.status = ?', 1)
                    ->where('article.reference_type = ?', M_DbTable_Article::REFERENCE_TYPE_CRUISE)
                    ->columns(array('id' 			=> 'cruise.id',
                    				'thumbnail'		=> 'article.thumbnail'));

        if($this->getLang() == 'en') {
            $q->columns(array('name'        => 'cruise.en_name',
            				  'overview'	=> 'article.overview',
            				  'type_title' 	=> 'cruise_type.title'));
            $q->order('cruise_type.id');
        } else {
        	$q->columns(array('name'      	=> 'cruise.name',
            				  'overview'	=> 'article.en_overview',
            				  'type_title' 	=> 'cruise_type.title'));
        	$q->order('cruise_type.id');
        }
        return $q;
    }

    //list cruise by categoryId, cruiseType for menu left
    public function listCruiseMenuLeft($categoryId, $typeId, $parentCruiseId = 0) {
    	$q = $this->_db
                    ->select()
                    ->from('cruise', null)
                    ->join('cruise_category', 'cruise_category.id = cruise.cruise_category_id', null)
                    ->join('cruise_type', 'cruise_type.id = cruise.cruise_type_id', null)
                    ->where('cruise_category.id = ?', $categoryId)
                    ->where('cruise_type.id = ?', $typeId)
                    ->where('cruise.status = ?', 1)
                    ->where('cruise.parent_cruise_id = ?', $parentCruiseId)
                    ->order('cruise.en_name')
                    ->columns(array('id' => 'cruise.id', 'parent_cruise_id' => 'cruise.parent_cruise_id'));

        if($this->getLang() == 'en') {
            $q->columns(array('name' => 'cruise.en_name'));
        } else {
        	$q->columns(array('name' => 'cruise.name'));
        }
        return $q;
    }

    public function getCruiseInfo($cruiseId) {
    	$q = $this->_db
			    	->select()
			    	->from('cruise', null)
			    	->joinLeft('cruise_category', 'cruise_category.id = cruise.cruise_category_id', null)
			    	->joinLeft('cruise_type', 'cruise_type.id = cruise.cruise_type_id', null)
			    	->joinLeft('article', 'article.reference_id = cruise.id AND article.reference_type = '. M_DbTable_Article::REFERENCE_TYPE_CRUISE, null)
			    	->where('cruise.id = ?', $cruiseId)
			    	->where('cruise.status = ?', 1)
			    	->columns(array('id' 			    => 'cruise.id',
        			    	        'meta_keyword'      => 'cruise.meta_keyword',
        			    	        'meta_description'  => 'cruise.meta_description',
			    					'header_title'		=> 'cruise.header_title',
					    			'detail_thumbnail'	=> 'cruise.detail_thumbnail',
					    			'category_id'	    => 'cruise_category.id',
					    			'thumbnail'		    => 'article.thumbnail',
					    			'description'	    => 'cruise.description'));

    	if($this->getLang() == 'en') {
    		$q->columns(array('name'        => 'cruise.en_name',
		    				  'itinerary'	=> 'cruise.en_itinerary',
		    				  'note_price'	=> 'cruise.en_note_price',
		    				  'overview'	=> 'article.en_overview',
		    				  'detail'		=> 'article.en_detail',
		    				  'cruise_category_title' => 'cruise_category.en_title'));
    	} else {
    		$q->columns(array('name'      	=> 'cruise.name',
		    				  'itinerary'	=> 'cruise.itinerary',
		    				  'note_price'	=> 'cruise.note_price',
		    				  'overview'	=> 'article.overview',
		    				  'detail'		=> 'article.detail',
		    				  'cruise_category_title' => 'cruise_category.title'));
    	}

    	return $q;
    }

    //list promotion cruise
    public function listPromotionCruise($limit) {
    	$q = $this->_db
			    	->select()
			    	->from('cruise', null)
			    	->join('cruise_category', 'cruise_category.id = cruise.cruise_category_id', null)
			    	->join('cruise_type', 'cruise_type.id = cruise.cruise_type_id', null)
			    	->join('article', 'article.reference_id = cruise.id', null)
			    	->where('cruise.special_offer = ?', 1)
			    	->where('cruise.status = ?', 1)
			    	->where('article.reference_type = ?', M_DbTable_Article::REFERENCE_TYPE_CRUISE)
			    	->columns(array('id' 			=> 'cruise.id',
					    			'category_id'	=> 'cruise_category.id',
					    			'thumbnail'		=> 'article.thumbnail'));
    	if($limit) {
    	    $q->limit($limit);
    	}

    	if($this->getLang() == 'en') {
    		$q->columns(array('title'        => 'cruise.en_name',
		    				  'overview'	=> 'article.en_overview'));
    	} else {
    		$q->columns(array('title'      	=> 'cruise.name',
		    				  'overview'	=> 'article.overview'));
    	}
    	return $q;
    }

    // LIST ACTIVE
	public function getListActive() {
		$q = $this->_db
				  ->select()
				  ->from(array('c'   => $this->_name), null)
				  ->where('c.status =? ', M_DbTable_Cruise::STATUS_ACTIVE)
				  ->order('c.created_date DESC')
				  ->columns(
				  		array('id'						=>  'c.id',
							  'name'					=>	'c.en_name',
				  ));
		return $q;
	}
}